package com.ruoyi.util.concretecheck;

/**
 * @author 62418
 * @since 2020/8/20 8:00
 */

import com.ruoyi.domain.jpjs.common.concretecheck.ConcreteCheck;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.FileOutputStream;
import java.util.List;

public class ExportConcreteExcel {
    public void getValue(List<ConcreteCheck> list, FileOutputStream fout){
        try{
            //1.创建工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //1.1创建合并单元格对象
            CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,32);//起始行,结束行,起始列,结束列

            // 标题
            CellRangeAddress callRangeAddress31 = new CellRangeAddress(1,2,0,0);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress32 = new CellRangeAddress(1,2,1,1);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress33 = new CellRangeAddress(1,2,2,2);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress34 = new CellRangeAddress(1,2,3,3);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress35 = new CellRangeAddress(1,2,4,4);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress36 = new CellRangeAddress(1,2,5,5);//起始行,结束行,起始列,结束列

            CellRangeAddress callRangeAddress37 = new CellRangeAddress(1,1,6,8);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress371 = new CellRangeAddress(1,1,9,11);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress372 = new CellRangeAddress(1,1,12,14);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress373 = new CellRangeAddress(1,1,15,17);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress374 = new CellRangeAddress(1,1,18,20);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress375 = new CellRangeAddress(1,1,21,23);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress376 = new CellRangeAddress(1,1,24,26);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress378 = new CellRangeAddress(1,1,27,29);//起始行,结束行,起始列,结束列

            CellRangeAddress callRangeAddress377 = new CellRangeAddress(1,2,30,30);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress40 = new CellRangeAddress(1,2,31,31);//起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress41 = new CellRangeAddress(1,2,32,32);//起始行,结束行,起始列,结束列

            // 项目混凝土强度试验检测统计表
            HSSFCellStyle headStyle = createCellStyle(workbook,(short)16,true,true);
            // 工程公司
            HSSFCellStyle erStyle = createCellStyle(workbook,(short)13,true,true);
            // 标题样式
            HSSFCellStyle colStyle = createCellStyle(workbook,(short)10,true,true);
            //内容样式
            HSSFCellStyle cellStyle = createCellStyle(workbook,(short)10,false,true);

            //2.创建工作表
            HSSFSheet sheet = workbook.createSheet("混凝土实测统计表");
            //2.1加载合并单元格对象
            sheet.addMergedRegion(callRangeAddress);
            sheet.addMergedRegion(callRangeAddress31);
            sheet.addMergedRegion(callRangeAddress32);
            sheet.addMergedRegion(callRangeAddress33);
            sheet.addMergedRegion(callRangeAddress34);
            sheet.addMergedRegion(callRangeAddress35);
            sheet.addMergedRegion(callRangeAddress36);

            sheet.addMergedRegion(callRangeAddress37);
            sheet.addMergedRegion(callRangeAddress371);
            sheet.addMergedRegion(callRangeAddress372);
            sheet.addMergedRegion(callRangeAddress373);
            sheet.addMergedRegion(callRangeAddress374);
            sheet.addMergedRegion(callRangeAddress375);
            sheet.addMergedRegion(callRangeAddress376);
            sheet.addMergedRegion(callRangeAddress377);
            sheet.addMergedRegion(callRangeAddress378);

            sheet.addMergedRegion(callRangeAddress40);
            sheet.addMergedRegion(callRangeAddress41);

            //设置默认列宽
            sheet.setDefaultColumnWidth(15);
            //3.创建行
            //3.1创建头标题行;并且设置头标题
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell = row.createCell(0);

            //加载单元格样式
            cell.setCellStyle(headStyle);
            cell.setCellValue("混凝土实测统计表");

            //3.2创建列标题;并且设置列标题
            HSSFRow row2 = sheet.createRow(1);
            //""为占位字符串
            String[] titles = {"序号","工程公司","项目名称","工程部位","结构部位","浇筑日期", "汇总","","","垂直度","","","平整度","","","截面尺寸","","","阴阳角","","","开间尺寸","","","顶板水平度","","","楼板厚度","","","备注","录入人员","检测人员"};
            for(int i=0;i<titles.length;i++)
            {
                HSSFCell cell2 = row2.createCell(i);
                //加载单元格样式
                cell2.setCellStyle(colStyle);
                cell2.setCellValue(titles[i]);
            }

            HSSFRow rowfour = sheet.createRow(2);
            String[] titlefour = {"检测点数","合格点数","合格率","检测点数","合格点数","合格率","检测点数","合格点数","合格率","检测点数","合格点数","合格率","检测点数","合格点数","合格率","检测点数","合格点数","合格率","检测点数","合格点数","合格率","检测点数","合格点数","合格率"};
            for(int i=0;i<titlefour.length;i++)
            {
                HSSFCell cell2 = rowfour.createCell(i+6);
                //加载单元格样式
                cell2.setCellStyle(colStyle);
                cell2.setCellValue(titlefour[i]);
            }

            //4.操作单元格;将用户列表写入excel
            if(list != null)
            {
                int i=1;
                for(int j=0;j<list.size();j++)
                {
                    //创建数据行,前面有两行,头标题行和列标题行
                    HSSFRow row3 = sheet.createRow(j+3);
                    HSSFCell cell0 = row3.createCell(0);
                    cell0.setCellStyle(cellStyle);
                    cell0.setCellValue(i++);

                    HSSFCell cell1 = row3.createCell(1);
                    cell1.setCellStyle(cellStyle);
                    cell1.setCellValue(list.get(j).getCompanyName());

                    HSSFCell cell2 = row3.createCell(2);
                    cell2.setCellStyle(cellStyle);
                    cell2.setCellValue(list.get(j).getProgramName());


                    HSSFCell cell3 = row3.createCell(3);
                    cell3.setCellStyle(cellStyle);
                    cell3.setCellValue(list.get(j).getBuildingNumber());

                    HSSFCell cell4 = row3.createCell(4);
                    cell4.setCellStyle(cellStyle);
                    cell4.setCellValue(list.get(j).getFloorNumber());

                    HSSFCell cell5 = row3.createCell(5);
                    cell5.setCellStyle(cellStyle);
                    cell5.setCellValue(list.get(j).getPouringTime());

                    // 汇总
                    HSSFCell cell6 = row3.createCell(6);
                    cell6.setCellStyle(cellStyle);
                    cell6.setCellValue(list.get(j).getTotalCheck());

                    HSSFCell cell7 = row3.createCell(7);
                    cell7.setCellStyle(cellStyle);
                    cell7.setCellValue(list.get(j).getTotalActual());

                    HSSFCell cell8 = row3.createCell(8);
                    cell8.setCellStyle(cellStyle);
                    cell8.setCellValue(list.get(j).getTotalRate());

                    // 垂直度
                    HSSFCell cell9 = row3.createCell(9);
                    cell9.setCellStyle(cellStyle);
                    cell9.setCellValue(list.get(j).getVerticalityCheck());

                    HSSFCell cell10 = row3.createCell(10);
                    cell10.setCellStyle(cellStyle);
                    cell10.setCellValue(list.get(j).getVerticalityActual());

                    HSSFCell cell11 = row3.createCell(11);
                    cell11.setCellStyle(cellStyle);
                    cell11.setCellValue(list.get(j).getVerticalityRate());

                    // 平整度
                    HSSFCell cell12 = row3.createCell(12);
                    cell12.setCellStyle(cellStyle);
                    cell12.setCellValue(list.get(j).getPlanenessCheck());

                    HSSFCell cell13 = row3.createCell(13);
                    cell13.setCellStyle(cellStyle);
                    cell13.setCellValue(list.get(j).getPlanenessActual());

                    HSSFCell cell14 = row3.createCell(14);
                    cell14.setCellStyle(cellStyle);
                    cell14.setCellValue(list.get(j).getPlanenessRate());

                    // 截面尺寸
                    HSSFCell cell15 = row3.createCell(15);
                    cell15.setCellStyle(cellStyle);
                    cell15.setCellValue(list.get(j).getSectionCheck());

                    HSSFCell cell16 = row3.createCell(16);
                    cell16.setCellStyle(cellStyle);
                    cell16.setCellValue(list.get(j).getSectionActual());

                    HSSFCell cell17 = row3.createCell(17);
                    cell17.setCellStyle(cellStyle);
                    cell17.setCellValue(list.get(j).getSectionRate());

                    // 阴阳角
                    HSSFCell cell18 = row3.createCell(18);
                    cell18.setCellStyle(cellStyle);
                    cell18.setCellValue(list.get(j).getAngleCheck());

                    HSSFCell cell19 = row3.createCell(19);
                    cell19.setCellStyle(cellStyle);
                    cell19.setCellValue(list.get(j).getAngleActual());

                    HSSFCell cell20 = row3.createCell(20);
                    cell20.setCellStyle(cellStyle);
                    cell20.setCellValue(list.get(j).getAngleRate());

                    // 开间尺寸
                    HSSFCell cell21 = row3.createCell(21);
                    cell21.setCellStyle(cellStyle);
                    cell21.setCellValue(list.get(j).getBayCheck());

                    HSSFCell cell22 = row3.createCell(22);
                    cell22.setCellStyle(cellStyle);
                    cell22.setCellValue(list.get(j).getBayActual());

                    HSSFCell cell23 = row3.createCell(23);
                    cell23.setCellStyle(cellStyle);
                    cell23.setCellValue(list.get(j).getBayRate());

                    // 顶板水平度
                    HSSFCell cell24 = row3.createCell(24);
                    cell24.setCellStyle(cellStyle);
                    cell24.setCellValue(list.get(j).getRoofCheck());

                    HSSFCell cell25 = row3.createCell(25);
                    cell25.setCellStyle(cellStyle);
                    cell25.setCellValue(list.get(j).getRoofActual());

                    HSSFCell cell26 = row3.createCell(26);
                    cell26.setCellStyle(cellStyle);
                    cell26.setCellValue(list.get(j).getRoofRate());

                    // 楼板厚度
                    HSSFCell cell27 = row3.createCell(27);
                    cell27.setCellStyle(cellStyle);
                    cell27.setCellValue(list.get(j).getFloorslabCheck());

                    HSSFCell cell28 = row3.createCell(28);
                    cell28.setCellStyle(cellStyle);
                    cell28.setCellValue(list.get(j).getFloorslabActual());

                    HSSFCell cell29 = row3.createCell(29);
                    cell29.setCellStyle(cellStyle);
                    cell29.setCellValue(list.get(j).getFloorslabRate());


                    HSSFCell cell30 = row3.createCell(30);
                    cell30.setCellStyle(cellStyle);
                    cell30.setCellValue(list.get(j).getNote());

                    HSSFCell cell31 = row3.createCell(31);
                    cell31.setCellStyle(cellStyle);
                    cell31.setCellValue(list.get(j).getCheckPerson());

                    HSSFCell cell32 = row3.createCell(32);
                    cell32.setCellStyle(cellStyle);
                    cell32.setCellValue(list.get(j).getTestPerson());
                }
            }
            //5.输出
            workbook.write(fout);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param workbook
     * @param fontsize
     * @return 单元格样式
     */
    private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontsize,boolean flag,boolean flag1) {
        // TODO Auto-generated method stub
        HSSFCellStyle style = workbook.createCellStyle();
        //是否水平居中
        if(flag1){
            style.setAlignment(HorizontalAlignment.CENTER);//水平居中
        }

        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        //创建字体
        HSSFFont font = workbook.createFont();
        //是否加粗字体
        if(flag){
            font.setBold(true);
        }
        font.setFontHeightInPoints(fontsize);
        //加载字体
        style.setFont(font);
        return style;
    }
}
